"""
<name>predict.lm</name>
<icon></icon>
"""
from OWRpy import * 
from libraries.base.qtWidgets.lineEdit import lineEdit as redRlineEdit 
from libraries.base.qtWidgets.radioButtons import radioButtons as redRradioButtons 
from libraries.base.qtWidgets.comboBox import comboBox as redRcomboBox 
from libraries.base.qtWidgets.checkBox import checkBox as redRcheckBox 
from libraries.base.qtWidgets.textEdit import textEdit as redRtextEdit 
from libraries.stats.signalClasses.RLMFit import RLMFit as redRRLMFit
from libraries.base.signalClasses.RMatrix import RMatrix as redRRMatrix
import libraries.base.signalClasses as signals

import redRi18n
_ = redRi18n.get_(package = 'compositions')


class RedRpredict_lm(OWRpy): 
    settingsList = []
    def __init__(self, **kwargs):
        OWRpy.__init__(self, **kwargs)
        self.setRvariableNames(["predict.lm"])
        self.data = {}

        self.inputs.addInput('id0', _('R lm Object'), redRRLMFit, self.processobject) #pyhton function(id, name, type, def function)
        self.outputs.addOutput("predict.lm Output",_("predict.lm Output"), redRRMatrix)
        
        self.RFunctionParamna_action_lineEdit = redRlineEdit(self.controlArea, label = "na_action:", text = 'na.pass')
        self.RFunctionParaminterval_lineEdit = redRlineEdit(self.controlArea, label = "interval:", text = 'c("none", "confidence", "prediction")')
        self.RFunctionParamterms_lineEdit = redRlineEdit(self.controlArea, label = "terms:", text = 'NULL')
        self.RFunctionParamse_fit_lineEdit = redRlineEdit(self.controlArea, label = "se_fit:", text = 'FALSE')
        self.RFunctionParamlevel_lineEdit = redRlineEdit(self.controlArea, label = "level:", text = '0.95')
        self.RFunctionParamdf_lineEdit = redRlineEdit(self.controlArea, label = "df:", text = 'Inf')
        self.RFunctionParamobject_lineEdit = redRlineEdit(self.controlArea, label = "object:", text = '')
        self.RFunctionParamscale_lineEdit = redRlineEdit(self.controlArea, label = "scale:", text = 'NULL')
        self.RFunctionParamweights_lineEdit = redRlineEdit(self.controlArea, label = "weights:", text = '1')
        self.RFunctionParampred_var_lineEdit = redRlineEdit(self.controlArea, label = "pred_var:", text = 'res.var/weights')
        self.RFunctionParamtype_lineEdit = redRlineEdit(self.controlArea, label = "type:", text = 'c("response","terms")')
        self.RFunctionParamnewdata_lineEdit = redRlineEdit(self.controlArea, label = "newdata:", text = '')
        self.commit = redRCommitButton(self.bottomAreaRight, _("Commit"), alignment=Qt.AlignRight, 
        callback = self.commitFunction, processOnInput=True)
        self.RoutputWindow = redRtextEdit(self.controlArea, label = _("R Output Window"))

    def processobject(self, data):
        if not self.require_librarys(["mvoutlier"]):
            self.status.setText('R Libraries Not Loaded.')
            return
        if data:
            self.RFunctionParam_object=data.getData()
            self.data = data
            if self.commit.processOnInput():
                self.commitFunction()
        else:
            self.RFunctionParam_object=''

    def commitFunction(self):
        
        injection = []
        if unicode(self.RFunctionParamna_action_lineEdit.text()) != '':
            string = ',na.action='+unicode(self.RFunctionParamna_action_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParaminterval_lineEdit.text()) != '':
            string = ',interval='+unicode(self.RFunctionParaminterval_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamterms_lineEdit.text()) != '':
            string = ',terms='+unicode(self.RFunctionParamterms_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamse_fit_lineEdit.text()) != '':
            string = ',se.fit='+unicode(self.RFunctionParamse_fit_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamlevel_lineEdit.text()) != '':
            string = ',level='+unicode(self.RFunctionParamlevel_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamdf_lineEdit.text()) != '':
            string = ',df='+unicode(self.RFunctionParamdf_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamobject_lineEdit.text()) != '':
            string = ',object='+unicode(self.RFunctionParamobject_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamscale_lineEdit.text()) != '':
            string = ',scale='+unicode(self.RFunctionParamscale_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamweights_lineEdit.text()) != '':
            string = ',weights='+unicode(self.RFunctionParamweights_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParampred_var_lineEdit.text()) != '':
            string = ',pred.var='+unicode(self.RFunctionParampred_var_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamtype_lineEdit.text()) != '':
            string = ',type='+unicode(self.RFunctionParamtype_lineEdit.text())+''
            injection.append(string)
        if unicode(self.RFunctionParamnewdata_lineEdit.text()) != '':
            string = ',newdata='+unicode(self.RFunctionParamnewdata_lineEdit.text())+''
            injection.append(string)
        inj = ''.join(injection)
        self.R(self.Rvariables['predict.lm']+'<-predict.lm(object='+self.RFunctionParam_object+','+inj+')')
        self.R('txt<-capture.output('+self.Rvariables['predict.lm']+')')
        self.RoutputWindow.clear()
        tmp = self.R('paste(txt, collapse ="\n")')
        self.RoutputWindow.insertHtml('<br>'+tmp+'')
        newData = redRRMatrix(self, data = self.Rvariables["predict.lm"]) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        #newData.copyAllOptinoalData(self.data)  ## note, if you plan to uncomment this please uncomment the call to set self.data in the process statemtn of the data whose attributes you plan to send forward.
        self.rSend("predict.lm Output", newData)